/*!
 * @file StateActuator.cpp
 * @author Rocco Martino
 */
/***************************************************************************
 *   Copyright (C) 2014 by Rocco Martino                                   *
 *   martinorocco@gmail.com                                                *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Lesser General Public License as        *
 *   published by the Free Software Foundation; either version 2.1 of the  *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU Lesser General Public      *
 *   License along with this program; if not, write to the                 *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

/* ======================================================================= */
/* ....................................................................... */
#include <ooGame/StateActuator>
#include <ooGame/Game>

#include <osgODE/Notify>
/* ....................................................................... */
/* ======================================================================= */




using namespace ooGame ;




/* ======================================================================= */
/* ....................................................................... */
StateActuator::StateActuator(Operation operation):
    m_operation             ( operation ),
    m_mask                  ( 0 ),
    m_waiting_false         ( false )
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
StateActuator::StateActuator(const StateActuator& other, const osg::CopyOp& copyop):
    Actuator                ( other, copyop ),
    m_operation             ( other.m_operation ),
    m_mask                  ( other.m_mask ),
    m_waiting_false         ( other.m_waiting_false )
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
StateActuator::~StateActuator(void)
{
}
/* ....................................................................... */
/* ======================================================================= */




/* ======================================================================= */
/* ....................................................................... */
void
StateActuator::operator()( osgODE::ODEObject* object, Game* game )
{
    (void) object ;

    PS_ASSERT1( game != NULL ) ;



    if( isActive() ) {

        if( ! m_waiting_false ) {


            unsigned int    state = game->getState() ;

            switch( m_operation )
            {
                case SET:
                    state = m_mask ;
                break ;


                case ADD:
                    state |= m_mask ;
                break ;


                case REMOVE:
                    state &= ~m_mask ;
                break ;


                case CHANGE:
                    state ^= m_mask ;
                break ;
            }



            game->setState( state ) ;


            m_waiting_false = true ;
        }



    } else { // isActive()

        m_waiting_false = false ;

    }
}
/* ....................................................................... */
/* ======================================================================= */
